package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacParamConstant;
import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.CollectionPayService;
import cc.rengu.igas.channel.upac.facade.request.CollectionSignRequest;
import cc.rengu.igas.channel.upac.facade.response.CollectionSignResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 银联全渠道代收签约服务 on 2019/10/8.
 */
public class CollectionSignService extends CollectionPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        CollectionSignRequest collectionSignRequest = new CollectionSignRequest();
        ConvertUtil.convertOutput(collectionSignRequest);
        return collectionSignRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        int iReturnCode = 0;
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        CollectionSignResponse collectionSignResponse = new CollectionSignResponse();
        CollectionSignRequest collectionSignRequest = (CollectionSignRequest) request;
        String payAcctNo = collectionSignRequest.getPayAcctNo();
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        if (AppParamConstant.YES.equals(acctEncFlag)) {
            collectionSignRequest.setPayAcctNo(encryptData(payAcctNo));
        }
        ConvertUtil.convertInput(collectionSignRequest);
        /*customer信息转换*/
        if (null != collectionSignRequest.getCustomerInfo()) {
            packageCustomerInfo(collectionSignRequest.getCustomerInfo(), collectionSignRequest.getPayAcctNo());
        }
        if (null != collectionSignRequest.getRiskCtrlInfo()) {
            packageRiskCtrlInfo(collectionSignRequest.getRiskCtrlInfo());
        }
        if (collectionSignRequest.getFrequency() == null || collectionSignRequest.getValidMonth() == null) {
            rglog.error("必须上送代收频率和期限");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            String reserved = "{validMonth=" + collectionSignRequest.getValidMonth() + "&frequency=" + collectionSignRequest.getFrequency() + "}";
            xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.reserved, reserved);
        }
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "72");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "16");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.DEFAULT.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, collectionSignRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            collectionSignResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            collectionSignResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(collectionSignResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(collectionSignResponse);
        collectionSignResponse.setRespCode(convertRespCode(collectionSignResponse.getRespCode()));

        if (collectionSignResponse.getPayAcctNo() != null) {
            collectionSignResponse.setPayAcctNo(payAcctNo);
        }
        chnlBizResponse.setResult(collectionSignResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        CollectionSignResponse collectionSignResponse = (CollectionSignResponse) response.getResult();
        ConvertUtil.convertInput(collectionSignResponse);
    }

    /**
     * 代收备用域转换处理
     *
     * @param collectionSignRequest 签约请求信息
     * @throws Exception 异常
     */
    protected void packageReserveField(CollectionSignRequest collectionSignRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        StringBuffer reserve = new StringBuffer(UpacParamConstant.LEFT_BRACE);
        if (null != collectionSignRequest.getMaxLimit() && !collectionSignRequest.getMaxLimit().isEmpty()) {
            reserve.append(UpacTreeNodeConstant.maxLimit + UpacParamConstant.EQUAL + collectionSignRequest.getMaxLimit() + UpacParamConstant.AMPERSAND);
        }
        if (null != collectionSignRequest.getMinLimit() && !collectionSignRequest.getMinLimit().isEmpty()) {
            reserve.append(UpacTreeNodeConstant.minLimit + UpacParamConstant.EQUAL + collectionSignRequest.getMinLimit() + UpacParamConstant.AMPERSAND);
        }
        if (null != collectionSignRequest.getValidMonth() && collectionSignRequest.getValidMonth().isEmpty()) {
            reserve.append(UpacTreeNodeConstant.validMonth + UpacParamConstant.EQUAL + collectionSignRequest.getValidMonth() + UpacParamConstant.AMPERSAND);
        }
        if (null != collectionSignRequest.getFrequency() && !collectionSignRequest.getFrequency().isEmpty()) {
            reserve.append(UpacTreeNodeConstant.frequency + UpacParamConstant.EQUAL + collectionSignRequest.getFrequency() + UpacParamConstant.AMPERSAND);
        }
        if (reserve.length() > 1) {
            reserve.setLength(reserve.length() - 1);
            reserve.append(UpacParamConstant.RIGHT_BRACE);
            xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.reserved, reserve.toString());
        }
    }
}
